<template>
  <div>
    <el-card :body-style="{padding: '10px'}">
      <el-row slot="header" type="flex" align="middle" justify="space-between">
        <div>
          <span class="my-iconfont-text1">房间灯光</span>
        </div>
        <div>
          <span>响应延时：</span>
          <span :class="delayClass">{{ delayMS }}</span>
        </div>
        <status-indicator :status="indicatorClass" :pulse="true" />
      </el-row>
      <el-row type="flex" justify="center" align="middle">
        <el-col :span="5">
          <el-row type="flex" justify="center" align="middle">
            <i class="iconfont icon-blub my-obj-icon" />
          </el-row>
        </el-col>
        <el-col :span="19">
          <el-row type="flex" justify="center" align="middle">
            <el-col :span="12">
              <el-row type="flex" justify="center" align="middle" style="margin-bottom: 10px">
                <el-select v-model="indexSelect" placeholder="选择房间" size="mini" @change="eventBlobRoomChange" :disabled="roomDisable">
                  <el-option
                    v-for="item in blobOption"
                    :key="item.index"
                    :label="item.label"
                    :value="item.index"
                  />
                  <div slot="prefix" class="my-selectPrefix">房间</div>
                </el-select>
              </el-row>
              <el-row type="flex" justify="center" align="middle" style="margin-top: 10px">
                <span>智能控制：</span>
                <i class="iconfont icon-naozi my-iconfont-no"></i>
              </el-row>
            </el-col>
            <el-col :span="12">
              <el-row type="flex" justify="center" align="middle" style="margin-bottom: 10px">
                <span>额外配置：</span><el-tag size="mini">颜色</el-tag>
              </el-row>
              <el-row type="flex" justify="center" align="middle" style="margin-top: 10px">
                <span>当前颜色：</span><el-color-picker v-model="color" @change="eventBlobColorChange" :disabled="colorDisable"></el-color-picker>
              </el-row>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>

import { blobRoom, blobColor } from "@/util/data/api-BlobControl"

export default {
  name: "BlobControl",
  data: function () {
    return {
      indexSelect: this.$store.state.blobControl.roomName,
      blobOption: [{
        index: '客厅',
        label: '客厅'
      }, {
        index: '卧室',
        label: '卧室',
      }, {
        index: '走廊',
        label: '走廊'
      }],
      color: this.$store.state.blobControl.colorCode,
      delayMs: null,
      delayClass: '',
      indicatorClass: '',
      roomDisable: false,
      colorDisable: false
    }
  },
  methods: {
    eventBlobRoomChange(room) {
      this.roomDisable = true
      const that = this
      const transeDataDict = {
        room: room
      }
      const lastTime = new Date().getTime()
      this.indicatorClass = 'intermediary'
      blobRoom(transeDataDict).then(res => {
        const { data } = res
        if (!data) {
          that.indicatorClass = 'negative'
          that.$store.state.blobControl.roomName = room
        } else {
          that.delayMs = Math.abs((data.time - lastTime).toFixed(2))
          that.indicatorClass = 'positive'
          that.$store.state.blobControl.roomName = data.room
        }
        that.roomDisable = false
      }).catch(() => {
        that.roomDisable = false
        that.indicatorClass = 'negative'
        that.$store.state.blobControl.roomName = room
      })
    },
    eventBlobColorChange(color) {
      this.colorDisable = true
      const that = this
      const transeDataDict = {
        color: color
      }
      const lastTime = new Date().getTime()
      this.indicatorClass = 'intermediary'
      blobColor(transeDataDict).then(res => {
        const { data } = res
        if (!data ) {
          that.indicatorClass = 'negative'
          that.$store.state.blobControl.colorCode = color
        } else {
          that.delayMs = Math.abs((data.time - lastTime).toFixed(2))
          that.indicatorClass = 'positive'
          that.$store.state.blobControl.colorCode = data.color
        }
        that.colorDisable = false
      }).catch(() => {
        that.colorDisable = false
        that.indicatorClass = 'negative'
        that.$store.state.blobControl.colorCode = color
      })
    }
  },
  computed: {
    delayMS() {
      return this.delayMs + ' ms'
    },
    heartBeat() {
      return this.$store.state.serverTime
    }
  },
  watch: {
    delayMs(val) {
      if (val <= 150) {
        this.delayClass = 'my-iconfont-greet'
      } else if (val <= 300) {
        this.delayClass = 'my-iconfont-normal'
      } else {
        this.delayClass = 'my-iconfont-bad'
      }
    },
    heartBeat() {
      this.indexSelect = this.$store.state.blobControl.roomName
      this.color = this.$store.state.blobControl.colorCode
    }
  }
}
</script>

<style scoped>
.my-iconfont-text1 {
  font-size: 25px;
  text-align: center;
  vertical-align: middle;
  color: orangered;
}
.my-obj-icon {
  text-align: center;
  font-size: 80px;
}

.my-iconfont-no {
  font-size: 34px;
}
.my-iconfont-yes {
  font-size: 34px;
  color: #409EFF;
}

.my-iconfont-greet {
  color: #42b983;
}
.my-iconfont-normal {
  color: greenyellow;
}
.my-iconfont-bad {
  color: orangered;
}
.my-selectPrefix {
  text-align: center;
  vertical-align: middle;
  line-height: 27px;
  color: #409EFF;
  font-weight: bold;
}
</style>
